package com.api.shopapi.mapper;



import com.api.shopapi.entity.Staff;
import com.api.shopapi.entity.dto.StaffDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
@Mapper
public interface StaffMapper {
    @Insert("insert into staff" +
            "(" +
            "StaffCode," +
            "ParentStaffCode," +
            "GovernmentCode," +
            "InstitutionCode," +
            "RoleCode," +
            "LoginName," +
            "Password," +
            "RealName," +
            "Phone," +
            "Status," +
            "CreateTime," +
            "LoginCount," +
            "LastLoginTime," +
            "ZoneCode," +
            "ZoneName," +
            "BusinessCode" +
            ")values(" +
            "#{staff.StaffCode}," +
            "#{staff.ParentStaffCode}," +
            "#{staff.GovernmentCode}," +
            "#{staff.InstitutionCode}," +
            "#{staff.RoleCode}," +
            "#{staff.LoginName}," +
            "#{staff.Password}," +
            "#{staff.RealName}," +
            "#{staff.Phone}," +
            "#{staff.Status}," +
            "#{staff.CreateTime}," +
            "#{staff.LoginCount}," +
            "#{staff.LastLoginTime}," +
            "#{staff.ZoneCode}," +
            "#{staff.ZoneName}," +
            "#{staff.BusinessCode}" +
            ")")
    int insert(@Param("staff") Staff staff);


    @Delete("delete from staff where staffCode=#{staffCode}")
    int deleteByStaffCode(@Param("staffCode") String staffCode);





    @Update("update staff set " +
            "RoleCode=#{staff.RoleCode}," +
            "ParentStaffCode=#{staff.ParentStaffCode}," +
            "GovernmentCode=#{staff.GovernmentCode}," +
            "InstitutionCode=#{staff.InstitutionCode}," +
            "LoginName=#{staff.LoginName}," +
            "Password=#{staff.Password}," +
            "RealName=#{staff.RealName}," +
            "Phone=#{staff.Phone}," +
            "Status=#{staff.Status}," +
            "LoginCount=#{staff.LoginCount}," +
            "LastLoginTime=#{staff.LastLoginTime}," +
            "ZoneCode=#{staff.ZoneCode}," +
            "ZoneName=#{staff.ZoneName}," +
            "BusinessCode=#{staff.BusinessCode}" +
            " where StaffCode=#{staff.StaffCode}")
    int updateByStaffCode(@Param("staff") Staff staff);




    @Select("select * from staff where staffCode=#{staffCode} ")
    Staff getByStaffCode(@Param("staffCode") String staffCode);

    @Select("select * from staff where loginName=#{loginName} limit 1")
    Staff getByLoginName(@Param("loginName") String loginName);

    @Select("select * from staff where phone=#{phone} limit 1")
    Staff getByPhone(@Param("phone") String phone);

    @Select("select * from staff where (loginName=#{loginName} and password=#{password})  limit 1")
    Staff getByLoginNamePassword(@Param("loginName") String loginName,@Param("password") String password);


    @Select("select s.* from staff s,role r where s.roleCode = r.roleCode and s.governmentCode=#{governmentCode} " +
            "and r.name=#{roleName} limit 1")
    Staff getByRoleNameGovernmentCode(@Param("roleName") String roleName,
                                              @Param("governmentCode") String governmentCode);


    @Select("select * from staff order by CreateTime desc")
    List<Staff> getList();

    @Select("select * from staff order by CreateTime desc")
    List<StaffDTO> getListStaffDTO();

    //获取政府内的所有职工
    @Select("select * from staff where governmentCode=#{governmentCode} order by CreateTime desc")
    List<StaffDTO> getListStaffDTOByGovernmentCode(@Param("zoneCode") String zoneCode);

    //获取政府内的所有职工
    @Select("select * from staff where governmentCode=#{governmentCode} order by CreateTime desc")
    List<StaffDTO> getListStaffByGovernmentCode(@Param("governmentCode") String governmentCode);


    //模糊查询 职工
    @Select("<script>" +
            "select * from staff " +
            "where governmentCode=#{governmentCode} " +
            "<if test ='realName !=\"\" '>" +
            "and RealName like concat('%',#{realName},'%')" +
            "</if>" +
            "<if test =' phone!=\"\" '>" +
            "and Phone like concat('%',#{phone},'%')" +
            "</if>" +
            " order by CreateTime desc "+
            "</script>")
    List<StaffDTO> getListForSelect(@Param("governmentCode") String governmentCode,
                                           @Param("realName") String realName,
                                           @Param("phone") String phone
                                           );


    //模糊查询 职工(只显示商家)
    @Select("<script>" +
            "select * from staff  as s  left join role as r on r.RoleCode=s.RoleCode    " +
            "where governmentCode=#{governmentCode}  and r.Name='商家'  " +
            "<if test ='realName !=\"\" '>" +
            "and RealName like concat('%',#{realName},'%')" +
            "</if>" +
            "<if test =' phone!=\"\" '>" +
            "and Phone like concat('%',#{phone},'%')" +
            "</if>" +
            " order by CreateTime desc "+
            "</script>")
    List<StaffDTO> getListForBusiness111(@Param("governmentCode") String governmentCode,
                                    @Param("realName") String realName,
                                    @Param("phone") String phone);


    //模糊查询 职工(不显示商家)
    @Select("<script>" +
            "select * from staff  as s  left join role as r on r.RoleCode=s.RoleCode    " +
            "where governmentCode=#{governmentCode}  and r.Name in ('省民政','市民政','县区民政','街道民政','居委会','组织账号') " +
            " " +
            "<if test ='realName !=\"\" '>" +
            "and RealName like concat('%',#{realName},'%')" +
            "</if>" +
            "<if test =' phone!=\"\" '>" +
            "and Phone like concat('%',#{phone},'%')" +
            "</if>" +
            " order by CreateTime desc "+
            "</script>")
    List<StaffDTO> getListForBusiness222(@Param("governmentCode") String governmentCode,
                                         @Param("realName") String realName,
                                         @Param("phone") String phone);









    @Select("<script>" +
            "select * from staff " +
            "where governmentCode=#{governmentCode} and ParentStaffCode=#{parentStaffCode} " +
            "<if test ='realName !=\"\" '>" +
            "and RealName like concat('%',#{realName},'%')" +
            "</if>" +
            "<if test =' phone!=\"\" '>" +
            "and Phone like concat('%',#{phone},'%')" +
            "</if>" +
            " order by CreateTime desc "+
            "</script>")
    List<StaffDTO> getListForBusiness(@Param("governmentCode") String governmentCode,
                                    @Param("realName") String realName,
                                    @Param("phone") String phone, @Param("parentStaffCode") String parentStaffCode);


    @Select("select s.* from staff s join role r on s.RoleCode = r.RoleCode where r.Name = #{roleName} and " +
            "s.governmentCode= #{governmentCode} order by s.CreateTime desc")
    List<Staff> getListByRoleNameGovernmentCode(@Param("roleName") String roleName,
                                                @Param("governmentCode") String governmentCode);


    //超级管理员只能查看系统管理员
    @Select("select * from staff as s left join role as r on r.RoleCode=s.RoleCode where r.Name='系统管理员' order by CreateTime desc")
    List<Staff> getListStaffForAdmin();

    @Select("select * from staff as s left join role as r on r.RoleCode=s.RoleCode where r.Name='政府管理员' order by CreateTime desc")
    List<StaffDTO> getListStaffForGovAdmin();

    @Select("select * from staff as s left join role as r on r.RoleCode=s.RoleCode where r.Name='客服' order by CreateTime desc")
    List<StaffDTO> getListStaffForSeats();

    //系统管理员查看未绑定客服职工
    @Select("<script>" +
            "SELECT s.RealName,s.StaffCode,s.LoginName FROM staff s,role r WHERE (select count(1) as num from seats where seats.StaffCode = s.StaffCode) = 0 AND GovernmentCode=#{governmentCode} and r.RoleCode=s.RoleCode and r.name='客服'" +
            "<if test =' staffCode!=\"\" '>" +
            " UNION SELECT s.RealName,s.StaffCode,s.LoginName from staff s WHERE StaffCode=#{staffCode} " +
            "</if>" +
            "</script>")
    List<StaffDTO> getUnboundForSeats(@Param("governmentCode") String governmentCode,@Param("staffCode") String staffCode);


    @Select("select * from staff where BusinessCode=#{businessCode} ")
    List<StaffDTO> getByBusinessCode(@Param("businessCode") String businessCode);



    @Select("select * from staff where GovernmentCode=#{governmentCode} and RoleCode=#{roleCode} ")
    List<Staff> getByGovernmentCodeAndRoleCode(@Param("governmentCode") String governmentCode,@Param("roleCode") String roleCode);




    //查询政府内某一类型的所有职工
    @Select("<script>" +
            "select s.StaffCode from staff s left join role r on r.RoleCode=s.RoleCode" +
            " where s.governmentCode=#{governmentCode} and r.Name = #{roleName} " +
            "<if test ='realName !=\"\" '>" +
            " and s.RealName =#{realName}" +
            "</if>" +
            "<if test =' phone!=\"\" '>" +
            " and s.Phone = #{phone}" +
            "</if>" +
            " order by s.CreateTime desc "+
            "</script>")
    List<Staff> listByGovCodeRoleName(@Param("governmentCode") String governmentCode,
                                      @Param("roleName") String roleName,
                                      @Param("realName") String realName,
                                      @Param("phone") String phone);


    //查询政府内某一类型的所有职工
    @Select("<script>" +
            "select s.StaffCode from staff s left join role r on r.RoleCode=s.RoleCode" +
            " where s.institutionCode=#{institutionCode} and r.Name = #{roleName} " +
            "<if test ='realName !=\"\" '>" +
            " and s.RealName =#{realName}" +
            "</if>" +
            "<if test =' phone!=\"\" '>" +
            " and s.Phone = #{phone}" +
            "</if>" +
            " order by s.CreateTime desc "+
            "</script>")
    List<Staff> listByInsCodeRoleName(@Param("institutionCode") String institutionCode,
                                      @Param("roleName") String roleName,
                                      @Param("realName") String realName,
                                      @Param("phone") String phone);


    @Select("select * from staff where RealName like   CONCAT('%',#{realName},'%')")
    List<Staff> query(@Param("realName") String realName);

}